// pages/high/high.js
const app = getApp();
const config = app.globalData.config;
const MD5 = require('../../utils/md5.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buy: false,
    vipModel: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var {
      buy
    } = options;
    !!buy && this.setData({
      buy: !!buy
    });
    this.getVipModel();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  onFAQ() {
    wx.navigateTo({
      url: '/pages/FAQ/FAQ'
    })
  },
  getVipModel() {
    var that = this;
    wx.request({
      url: config.vipModelUrl,
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'GET',
      success: function(res) {
        if (res.data.result == 1) {
          that.setData({
            vipModel: res.data.data
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  onPayment(e) {
    var that = this;
    var userInfo = wx.getStorageSync('sUserInfo');
    if (!userInfo) {
      wx.navigateTo({
        url: '/pages/me/me',
      })
      wx.showToast({
        title: '请授权',
        icon: 'none'
      })
    }
    wx.showLoading({
      title: '订单生成中',
      mask: true
    })
    var choose = e.currentTarget.dataset.mode;
    wx.request({
      url: config.paymentUrl,
      data: {
        userId: userInfo.id,
        vipModel: choose.code
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: 'POST',
      success: function(res) {

        if (res.data.result == 1) {
          var timestamp = (Date.parse(new Date()) / 1000) + '';
          var noncestr = Math.floor(Math.random() * 1000) + '';
          var ss = `appId=${config.appId}&nonceStr=${noncestr}&package=prepay_id=${res.data.data.prepay_id}&signType=MD5&timeStamp=${timestamp}&key=${config.mchKey}`;
          var sign = MD5.hexMD5(ss).toUpperCase();
          //发起微信支付
          wx.requestPayment({
            timeStamp: timestamp,
            nonceStr: noncestr,
            package: 'prepay_id=' + res.data.data.prepay_id,
            signType: 'MD5',
            paySign: sign,
            success: function(resp) {
              wx.request({
                url: config.payCallbackUrl,
                data: {
                  payId: res.data.data.payId,
                  userId: userInfo.id
                },
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                success: function(re) {
                  if (re.data.result == 1) {
                    app.getSUserInfo();
                    wx.showModal({
                      content: '支付成功',
                      showCancel: false
                    })
                    wx.navigateBack({
                      delta: 0
                    })
                  } else {
                    wx.showModal({
                      content: '处理异常，请联系客服',
                      showCancel: false
                    })
                  }
                },
                fail: function(res) {},
                complete: function(res) {},
              })
            },
            fail: function(resp) {
              wx.request({
                url: config.payCallbackUrl,
                data: {
                  payId: res.data.data.payId,
                  userId: userInfo.id,
                  isCancel: true
                },
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                method: 'POST',
                success: function(re) {
                  if (re.data.result == 1) {
                    wx.showModal({
                      content: '支付已取消',
                      showCancel: false
                    })
                  } else {
                    wx.showModal({
                      content: '处理异常，请联系客服',
                      showCancel: false
                    })
                  }
                },
                fail: function(res) {},
                complete: function(res) {},
              })
            },
            complete: function(resp) {
              wx.hideLoading()
            }
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {
        wx.hideLoading()
      },
    })
  }
})